from django.db import models

# Create your models here.
from users.models import User


class Category(models.Model):
    """分类导航模型"""
    name = models.CharField(max_length=128, verbose_name="分类名")

    class Meta:
        db_table = "tb_category"
        verbose_name = '文章分类'
        verbose_name_plural = verbose_name

class News(models.Model):
    """新闻模型"""
    new_img = models.CharField(max_length=128, default="./img/widget-photo.png",verbose_name="新闻图片")
    title = models.CharField(max_length=128, verbose_name="标题")
    content = models.TextField(verbose_name="正文")
    digest = models.TextField(max_length=128, null=True, verbose_name="摘要")
    create_time = models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
    clicks = models.IntegerField(default=1, verbose_name="点击量")
    user = models.ForeignKey(to='users.User', on_delete=models.PROTECT, verbose_name="用户")
    collect = models.IntegerField(default=0, verbose_name="收藏量")
    follow = models.IntegerField(default=0, verbose_name="关注")
    category = models.ForeignKey(Category, on_delete=models.PROTECT, verbose_name="分类")
    disinclination = models.BooleanField(default=0, verbose_name="不感兴趣")

    def __str__(self):
        return self.title

    class Meta:
        db_table = "tb_news"
        verbose_name = '新闻信息'
        verbose_name_plural = verbose_name


class NewsComment(models.Model):
    """新闻详情评论模型"""
    content = models.TextField(verbose_name='评论内容')
    news = models.ForeignKey(to='News', null=True, related_name='news_comments', verbose_name='新闻') #
    user = models.ForeignKey('users.User', null=True, related_name='user_comments', verbose_name='作者')
    count = models.IntegerField(default=0, verbose_name='点赞数')
    username = models.CharField(max_length=32, null=True, blank=True, verbose_name='作者名')
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='news_subs',
                                verbose_name='父评论')

    class Meta:
        db_table = 'tb_news_comment'
        verbose_name = '新闻评论'
        verbose_name_plural = verbose_name
        # managed = False
    # def __str__(self):
    #     return self.news_comment.title






